package com.dev.elasticsearch.demo;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Objects;

/**
 * @ClassName : RhlcDemo  //类名
 * @Description :   //描述
 * @Author :   //作者
 * @Date: 2021-08-18 11:05  //时间
 */
@RestController
@RequestMapping("/rhlc")
public class RhlcDemo {

    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;

    @GetMapping("createIndex/{index}")
    public ResponseEntity createIndex(@PathVariable("index") String index) {
        CreateIndexRequest cir = new CreateIndexRequest(index);
        CreateIndexResponse createIndexResponse = null;
        try {
            createIndexResponse = client.indices().create(cir, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(createIndexResponse);
    }

    @GetMapping("existsIndex/{index}")
    public ResponseEntity existsIndex(@PathVariable("index") String index) {
        boolean exists = false;
        try {
            GetIndexRequest gir = new GetIndexRequest(index);
            exists = client.indices().exists(gir, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(exists);
    }

    @DeleteMapping("deleteIndex/{index}")
    public ResponseEntity deleteIndex(@PathVariable("index") String index) {
        DeleteIndexRequest dr = new DeleteIndexRequest(index);
        AcknowledgedResponse delete = null;
        boolean exists = false;
        try {
            GetIndexRequest gir = new GetIndexRequest(index);
            exists = client.indices().exists(gir, RequestOptions.DEFAULT);
            if (exists){
                delete = client.indices().delete(dr, RequestOptions.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(Objects.nonNull(delete) ? delete.isAcknowledged() : false);
    }

    @PostMapping("create/")
    public ResponseEntity create(@RequestBody String index) {
        DeleteIndexRequest dr = new DeleteIndexRequest(index);
        AcknowledgedResponse delete = null;
        boolean exists = false;
        try {
            GetIndexRequest gir = new GetIndexRequest(index);
            exists = client.indices().exists(gir, RequestOptions.DEFAULT);
            if (exists){
                delete = client.indices().delete(dr, RequestOptions.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(Objects.nonNull(delete) ? delete.isAcknowledged() : false);
    }
}
